//
//  CouponsViewController.m
//  JiuJiuShangCheng
//
//  Created by jiujiukeji on 2018/7/25.
//  Copyright © 2018年 jiujiukeji. All rights reserved.
//

#import "SelectCouponsViewController.h"
#import "SGSegmentedControl.h"
#import "CouponsTableViewCell.h"

@interface SelectCouponsViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation SelectCouponsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = tableViewBgColor;
    [self loadBaseTitleLable:@"我的优惠券"];
    
    [self.view addSubview:self.tableView];
    [self getGoodsListData]; // 1未使用 2已使用
}
#pragma mark - 获取优惠券
- (void)getGoodsListData
{
    NSString *code = [NSString stringWithFormat:@"%@%@",safeCode,[GetTimestamp getNowTimeTimestamp]];
    [MBProgressHUD showMessage:nil toView:self.view];
    [TFNetworking getWithURLString:[NSString stringWithFormat:@"%@/api/json/member/vouchers.ashx?action=couponlist&companyid=%@&code=%@&timestamp=%@&pagesize=100000&pageindex=1&status=%@&money=%@",kServerURL,companyId,[MD5Encrypt  MD5ForLower32Bate:code],[GetTimestamp getNowTimeTimestamp],@"1",@(self.sumPrice)] parameters:nil success:^(id responseObject) {
        [MBProgressHUD hideHUDForView:self.view];
        self.dataArray = [NSMutableArray array];
        if ([responseObject[@"status"] intValue] == 1) {
            NSArray *array = responseObject[@"list"];
            for (NSDictionary *dic in array) {
                [self.dataArray addObject:dic];
            }
        }
        [self.tableView reloadData];
        
    } failure:^(NSError *error) {
        [TFHUD addRemindHUD:self.view message:@"网络不给力..."];
    }];
}
#pragma mark - tableView delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CouponsTableViewCell *cell = [[CouponsTableViewCell alloc] init];
    // 未使用
    cell.bgImg.image = UIImageNamed(@"couponWeishiyong"); // couponYishiyong couponShixiao
    // 赋值
    if (_dataArray.count != 0) {
        NSDictionary *dic = _dataArray[indexPath.row];
        cell.moneyLB.text = [NSString stringWithFormat:@"%@",dic[@"Qmoney"]];
        cell.conditionLB.text = dic[@"Atitle"];
        cell.describeLB.text = dic[@"QzjContent"];
        cell.timeLB.text = [NSString stringWithFormat:@"%@-%@",dic[@"SnAddTime"],dic[@"SnEndTime"]];
    }
    cell.selectionStyle = NO;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return rateHeight(260);
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_dataArray.count != 0) {
        NSDictionary *dic = _dataArray[indexPath.row];
        [self.delegate sendCouponsDic:dic];
        [self.navigationController popViewControllerAnimated:YES];
    }
}
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, SafeAreaTopHeight, kScreenWidth, kScreenHeight-SafeAreaTopHeight) style:(UITableViewStyleGrouped)];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = NO;
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
    }
    return _tableView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
